<?php
declare (strict_types=1);

namespace app\api\controller;

use app\model\BannersModel;
use app\model\InnerTestConfigModel;
use app\model\InnerTestUserModel;
use app\model\PostWhiteUserModel;
use think\App;

class InnerTest extends Base
{
    private $innerTestConfigModel;

    private $innerTestUserModel;

    private $postWhiteUserModel;

    private $uid;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->innerTestConfigModel = new InnerTestConfigModel();

        $this->innerTestUserModel = new InnerTestUserModel();

        $this->postWhiteUserModel = new PostWhiteUserModel();
    }

    /*
     * 获取所有banner
     * */
    public function checkTestPermissions()
    {
        $this->uid = $this->getUid();

        $config = $this->innerTestConfigModel->getInfo();
        $data = [
            "is_all_open" => $config["is_all_open"],
            "is_white" => -1,
            "isPoster"=>-1,
            "auditValue" => $config["auditValue"]
        ];

        if (!$this->uid) {
            return $this->succ($data);
        }


        if($this->postWhiteUserModel->isWhiteUser($this->uid)){
            $data["isPoster"] = 1;
        }


        if ($config["is_all_open"]) {
            $isWhite = $this->innerTestUserModel->isWhiteUser($this->uid);
            $data["is_white"] = $isWhite["accept_status"] == 1 ? 1 : -1;
        }


        return $this->succ($data);
    }


    public function applyFor()
    {

        $this->uid = $this->isLogin();

        $req = request()->param();

        if (!isset($req['wx']) || empty($req['wx'])) {
            return $this->fail("请输入微信号");
        }

        if ($this->innerTestUserModel->where("wx", $req['wx'])->find()) {
            return $this->fail("该微信号已申请");
        }

        $data = [
            "wx" => $req['wx'],
            "uid" => $this->uid,
            "accept_status" => 0,
            "created_at" => date("Y-m-d H:i:s"),
            "updated_at" => date("Y-m-d H:i:s"),
        ];

        $res = $this->innerTestUserModel->save($data);

        if ($res) {
            return $this->succ();
        } else {
            return $this->fail();
        }

    }
}
